Telegram AI Langchain bot

工作流概述

这是一个包含0个节点的未知工作流,主要用于自动化处理各种任务。

工作流源代码

下载
{
  "id": "ax8PJlp1UDb6EGFt",
  "meta": {
    "instanceId": "fb924c73af8f703905bc09c9ee8076f48c17b596ed05b18c0ff86915ef8a7c4a"
  },
  "name": "Telegram AI Langchain bot",
  "tags": [],
  "nodes": [
    {
      "id": "e275f31f-6a5f-4444-8bf7-6c003a8e53df",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        1100,
        600
      ],
      "parameters": {
        "model": "gpt-4-1106-preview",
        "options": {
          "temperature": 0.7,
          "frequencyPenalty": 0.2
        }
      },
      "credentials": {
        "openAiApi": {
          "id": "63",
          "name": "OpenAi account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "f25a6666-ff23-4372-afd0-4920a99aab6a",
      "name": "Window Buffer Memory",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "position": [
        1220,
        600
      ],
      "parameters": {
        "sessionKey": "=chat_with_{{ $('Listen for incoming events').first().json.message.chat.id }}",
        "contextWindowLength": 10
      },
      "typeVersion": 1
    },
    {
      "id": "96faef5d-0349-47fe-a7cf-150953490e90",
      "name": "Telegram",
      "type": "n8n-nodes-base.telegram",
      "onError": "continueErrorOutput",
      "position": [
        1500,
        380
      ],
      "parameters": {
        "text": "={{ $json.output }}",
        "chatId": "={{ $('Listen for incoming events').first().json.message.from.id }}",
        "additionalFields": {
          "parse_mode": "HTML",
          "appendAttribution": false
        }
      },
      "credentials": {
        "telegramApi": {
          "id": "70",
          "name": "Telegram sdfsdfsdfsdfsfd_bot"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "5ad43039-aaa6-43cd-9b0f-1d02f4d9c4ff",
      "name": "Correct errors",
      "type": "n8n-nodes-base.telegram",
      "position": [
        1700,
        380
      ],
      "parameters": {
        "text": "={{ $('AI Agent').item.json.output.replace(/&/g, \"&amp;\").replace(/>/g, \"&gt;\").replace(/</g, \"&lt;\").replace(/\"/g, \"&quot;\") }}",
        "chatId": "={{ $('Listen for incoming events').first().json.message.from.id }}",
        "additionalFields": {
          "parse_mode": "HTML",
          "appendAttribution": false
        }
      },
      "credentials": {
        "telegramApi": {
          "id": "70",
          "name": "Telegram sdfsdfsdfsdfsfd_bot"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "0349a250-966a-4064-970a-8bcfba1647ad",
      "name": "Execute Workflow Trigger",
      "type": "n8n-nodes-base.executeWorkflowTrigger",
      "position": [
        940,
        900
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "69a45c1f-838f-49ce-9b89-75db6a8b876f",
      "name": "Listen for incoming events",
      "type": "n8n-nodes-base.telegramTrigger",
      "position": [
        940,
        380
      ],
      "webhookId": "322dce18-f93e-4f86-b9b1-3305519b7834",
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {}
      },
      "credentials": {
        "telegramApi": {
          "id": "70",
          "name": "Telegram sdfsdfsdfsdfsfd_bot"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "2f5d5f25-9870-40d6-ad42-52750e62de63",
      "name": "Send back an image",
      "type": "n8n-nodes-base.telegram",
      "position": [
        1300,
        900
      ],
      "parameters": {
        "file": "={{ $json.data[0].url }}",
        "chatId": "={{ $('Execute Workflow Trigger').first().json.chat_id }}",
        "operation": "sendPhoto",
        "additionalFields": {
          "parse_mode": "HTML"
        }
      },
      "credentials": {
        "telegramApi": {
          "id": "70",
          "name": "Telegram sdfsdfsdfsdfsfd_bot"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "50b43dbf-39e3-4d00-8e47-01e8c193cd1a",
      "name": "add response field",
      "type": "n8n-nodes-base.set",
      "position": [
        1480,
        900
      ],
      "parameters": {
        "fields": {
          "values": [
            {
              "name": "response",
              "stringValue": "Success"
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 3.2
    },
    {
      "id": "171bee83-c8e1-4af3-9d1c-12cb6ede4943",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        900,
        840
      ],
      "parameters": {
        "width": 752.0361990950231,
        "height": 247.42081447963798,
        "content": "## Generate an image with Dall-E 3 and send it via Telegram"
      },
      "typeVersion": 1
    },
    {
      "id": "4d81d201-70bf-4c80-9689-4b65851ad770",
      "name": "Dall-E 3 Tool",
      "type": "@n8n/n8n-nodes-langchain.toolWorkflow",
      "position": [
        1360,
        600
      ],
      "parameters": {
        "name": "Draw_Dalle_image",
        "fields": {
          "values": [
            {
              "name": "chat_id",
              "stringValue": "={{ $('Listen for incoming events').first().json.message.chat.id }}"
            }
          ]
        },
        "workflowId": "={{ $workflow.id }}",
        "description": "Call this tool to request a Dall-E 3 model, when the user asks to draw something. Please send the user request for an image as an inline query string."
      },
      "typeVersion": 1
    },
    {
      "id": "39d532d3-8c96-4722-9cb0-cad92ff39e94",
      "name": "Generate image in Dall-E 3",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1120,
        900
      ],
      "parameters": {
        "url": "https://api.openai.com/v1/images/generations",
        "method": "POST",
        "options": {},
        "sendBody": true,
        "authentication": "predefinedCredentialType",
        "bodyParameters": {
          "parameters": [
            {
              "name": "model",
              "value": "dall-e-3"
            },
            {
              "name": "prompt",
              "value": "={{ $json.query }}"
            },
            {
              "name": "n",
              "value": 1
            },
            {
              "name": "size",
              "value": "1024x1024"
            }
          ]
        },
        "nodeCredentialType": "openAiApi"
      },
      "credentials": {
        "openAiApi": {
          "id": "63",
          "name": "OpenAi account"
        }
      },
      "typeVersion": 4.1
    },
    {
      "id": "e5aa496d-55d3-456b-82bb-fe10a06c7338",
      "name": "AI Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        1140,
        380
      ],
      "parameters": {
        "text": "={{ $json.message.text }}",
        "options": {
          "humanMessage": "TOOLS
------
Assistant can ask the user to use tools to look up information that may be helpful in answering the users original question. The tools the human can use are:

{tools}

{format_instructions}

USER'S INPUT
--------------------
Here is the user's input (remember to respond with a markdown code snippet of a json blob with a single action, and NOTHING else):

{{input}}",
          "systemMessage": "=You are a helpful AI assistant. You are chatting with the user named `{{ $json.message.from.first_name }}`. Today is {{ DateTime.fromISO($now).toLocaleString(DateTime.DATETIME_FULL) }}

From time to time call a user by name (if the user name is provided). In your reply, always send a message in Telegram-supported HTML format. Here are the formatting instructions:
1. The following tags are currently supported:
<b>bold</b>, <strong>bold</strong>
<i>italic</i>, <em>italic</em>
<u>underline</u>, <ins>underline</ins>
<s>strikethrough</s>, <strike>strikethrough</strike>, <del>strikethrough</del>
<span class=\"tg-spoiler\">spoiler</span>, <tg-spoiler>spoiler</tg-spoiler>
<b>bold <i>italic bold <s>italic bold strikethrough <span class=\"tg-spoiler\">italic bold strikethrough spoiler</span></s> <u>underline italic bold</u></i> bold</b>
<a href=\"http://www.example.com/\">inline URL</a>
<code>inline fixed-width code</code>
<pre>pre-formatted fixed-width code block

功能特点

  • 自动检测新邮件
  • AI智能内容分析
  • 自定义分类规则
  • 批量处理能力
  • 详细的处理日志

技术分析

节点类型及作用

    复杂度评估

    配置难度:
    ★☆☆☆☆
    维护难度:
    ★★☆☆☆
    扩展性:
    ★★★★☆

    实施指南

    前置条件

    • 有效的Gmail账户
    • n8n平台访问权限
    • Google API凭证
    • AI分类服务订阅

    配置步骤

    1. 在n8n中导入工作流JSON文件
    2. 配置Gmail节点的认证信息
    3. 设置AI分类器的API密钥
    4. 自定义分类规则和标签映射
    5. 测试工作流执行
    6. 配置定时触发器(可选)

    关键参数

    参数名称 默认值 说明
    maxEmails 50 单次处理的最大邮件数量
    confidenceThreshold 0.8 分类置信度阈值
    autoLabel true 是否自动添加标签

    最佳实践

    优化建议

    • 定期更新AI分类模型以提高准确性
    • 根据邮件量调整处理批次大小
    • 设置合理的分类置信度阈值
    • 定期清理过期的分类规则

    安全注意事项

    • 妥善保管API密钥和认证信息
    • 限制工作流的访问权限
    • 定期审查处理日志
    • 启用双因素认证保护Gmail账户

    性能优化

    • 使用增量处理减少重复工作
    • 缓存频繁访问的数据
    • 并行处理多个邮件分类任务
    • 监控系统资源使用情况

    故障排除

    常见问题

    邮件未被正确分类

    检查AI分类器的置信度阈值设置,适当降低阈值或更新训练数据。

    Gmail认证失败

    确认Google API凭证有效且具有正确的权限范围,重新进行OAuth授权。

    调试技巧

    • 启用详细日志记录查看每个步骤的执行情况
    • 使用测试邮件验证分类逻辑
    • 检查网络连接和API服务状态
    • 逐步执行工作流定位问题节点

    错误处理

    工作流包含以下错误处理机制:

    • 网络超时自动重试(最多3次)
    • API错误记录和告警
    • 处理失败邮件的隔离机制
    • 异常情况下的回滚操作